<?php

namespace app\model;

use think\facade\Db;
use think\Model;

class OrderAftersalesModel extends Model {

    protected $table ="order_aftersales";

    protected $pk = 'order_after_sale_id';

    private $cacheKey="order:aftersale:id:";
    private $cacheTime=5184000;


    /*
     * 检查订单是否存在处理中的售后订单
     * */
    public function isRunOrderAftersales($order_id){
        $where[] = ["order_id","=",$order_id];
        $where[] = ["aftersale_status","<>",-1];
        return $this->getList($where);
    }



    /*
     * 检查订单是否存在处理中的联合售后订单
     * */
    public function isRunPorderAftersales($p_order_id){
        $where[] = ["order_id","=",$p_order_id];
        $where[] = ["aftersale_status","<>",-1];
        return $this->getList($where);
    }

    /*
     * 获取售后单状态
     * */
    public function getOrderAftersalesStatus($order_after_sale_id){
        $where[] = ["order_after_sale_id","=",$order_after_sale_id];
        return Db::name('order_aftersales')->where($where)->value("aftersale_status");
    }


    /*
     *添加申请售后记录
     * */
    public function addOne($data){
        return Db::name('order_aftersales')->insertGetId($data);
    }

    /*
     *更新申请售后记录
     * */
    public function updateOrderAftersales($order_after_sale_id,$data){
        $where[] = ["order_after_sale_id",'=',$order_after_sale_id];
        return Db::name('order_aftersales')->where($where)->update($data);
    }




    /*
    * 获取total
    * */
    public function getTotal($where=[]){
        return Db::name('order_aftersales')->where($where)->count();
    }


    /*
     * 获取list
     * */
    public function getList($where=[],$page = null,$limit = null,$orderField = null,$orderType = "ASC"){

        $obj =  Db::name('order_aftersales')->field("order_after_sale_id")->where($where);

        if(!empty($page) && !empty($limit)){
            $obj = $obj->page($page, $limit);
        }

        if(!empty($orderField) && !empty($orderType)){
            $obj = $obj->order($orderField, $orderType);
        }

        $one = $obj->find();

        if($one){
            return $this->getInfo($one['order_after_sale_id']);
        }
        return [];
    }



    /*
   * 获取订单状态详情
   * */
    public function getInfo($id){
        $cacheKey = $this->cacheKey.$id;
        $redisModel = new RedisModel();
        $info = $redisModel->get($cacheKey);
        if($info){
            $info = json_decode($info,true);
        }else{
            $info = Db::name('order_aftersales')->find($id);
            $info["format_order_total_amount"]= round(  $info["order_total_amount"] / 100,2);
            $info["format_user_refund_amount"]= round(  $info["user_refund_amount"] / 100,2);
            $info['aftersale_thumbnail'] = !empty($info['aftersale_thumbnail']) ? json_decode($info['aftersale_thumbnail'],true) : "";
            $info['wx_return_info'] = !empty($info['wx_return_info']) ? json_decode($info['wx_return_info'],true) : "";
            $redisModel->set($cacheKey,json_encode($info,256),$this->cacheTime);
        }

        if($info){
            $info["aftersale_status"] = $this->getAftersaleStatus($id);
            $info["aftersale_status_info"] = (new BaseOrderAftersalesStatus())->getInfo($info["aftersale_status"]);
            $info["shop_remaining_time"] = strtotime($info["created_at"]) + 24 * 3600 - time();
            return $info;
        }
        return $info;
    }



    /*
  * 获取订单状态
  * */
    public function getAftersaleStatus($order_after_sale_id){
        if(empty($order_after_sale_id) ){
            return false;
        }
        $where=[];
        $where[] = ["order_after_sale_id","=",$order_after_sale_id];
        return Db::name('order_aftersales')->where($where)->value("aftersale_status");
    }

}